/**
 * Creates character Creation UI
 */
package View;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;

/**
 * * This class is the screen to be displayed 
 * in the event of a new game. It allows the player
 * to set their skill points, character name and gender, 
 * and select a difficulty
 * @author Whitney
 * @version 1.0
 *
 */
public class MakePlayerWindow extends JPanel {
	
	/**
	 * Name Field
	 */
	private final JTextField textField;

	/**
	 * All character creation JLabels
	 */
	private final JLabel lbldiffCheck, lblpointsLeft, lblPilotPoints, lblEngineerPoints, 
		lblFighterPoints, lblTraderPoints;

	/**
	 * All character creation Buttons
	 */
	private final JButton btnEasy, btnNormal, btnHard, btnBrutal, btnCharacterComplete;

	/**
	 * All skill selection Sliders
	 */
	private final JSlider engineerSlider, pilotSlider, traderSlider, fighterSlider;

	/**
	 * Gender Selection Radio Buttons
	 */
	private final JRadioButton rdbtnMale, rdbtnFemale;

	/**
	 * Male Image Icon
	 */
	private final ImageIcon spaceMan = new ImageIcon ("images/LilSpace.png");

	/**
	 * Female Image Icon
	 */
	private final ImageIcon spaceGal = new ImageIcon ("images/LilSpaceG.png");

	/**
	 * Initial Player Name
	 */
	private String playerName = "";

	/**
	 * Initial Difficulty
	 */
	private int difficulty = -1;

	/**
	 * Initial Gender
	 */
	private boolean boolFemale = false;

	/**
	 * Initial Gender
	 */
	private boolean boolMale = false;

	/**
	 * Array of Skill Values
	 */
	private final int[] skillsArray = new int[4];

	/**
	 * Initial Return Value
	 */
	private int charComplete = 0;
	
	/**
	 * Initial Slider Min
	 */
	private static final int SLIDERSMIN = 0;

	/**
	 * Initial SLider Max
	 */
	private static final int SLIDERSMAX = 20;

	/**
	 * Slider Starting Positions
	 */
	private static final int SLIDERSINIT = 0;

	/**
	 * Slider Spacing
	 */
	private static final int MINORTICKSPACING = 1;

	/**
	 * Slider Int Spacing
	 */
	private static final int MAINTICKSPACING = 5;
	
	/**
	 * Initial Skill Points
	 */
	private int allocatedPoints = 0;

	/**
	 * Initial amount of points to be spent
	 */
	private Integer pointsLeft = 0;

	/**
	 * Initial skill Points
	 */
	private Integer pilotPoints, engineerPoints, fighterPoints, traderPoints;

	/**
	 * Spaceholder for character Image
	 */
	private final JLabel lblCharimghere;
	
	/**
	 * The MakePlayerWindow JPanel handles the character creation phase. It takes
	 * the character's name and gender, and allows the player to select difficulty
	 * and assign skill points
	 */
	public MakePlayerWindow() {
		setBackground(Color.BLACK);
		setLayout(null);

//Jlabels
		lbldiffCheck = new JLabel("");
		lbldiffCheck.setBounds(259, 14, 0, 0);
		lbldiffCheck.setFont(new Font("Krungthep", Font.PLAIN, 17));
		lbldiffCheck.setForeground(Color.ORANGE);
add(lbldiffCheck);
		final JLabel lblCharImage = new JLabel("Character Image");
		lblCharImage.setBounds(156, 0, 8, 5);
add(lblCharImage);
		final JLabel lblDifficulty = new JLabel("Difficulty");
		lblDifficulty.setBounds(376, 5, 68, 19);
		lblDifficulty.setForeground(Color.ORANGE);
		lblDifficulty.setFont(new Font("Krungthep", Font.PLAIN, 13));
add(lblDifficulty);
		final JLabel lblCharName = new JLabel("Character Name");
		lblCharName.setBounds(8, 14, 148, 29);
		lblCharName.setFont(new Font("Krungthep", Font.PLAIN, 17));
		lblCharName.setForeground(Color.ORANGE);
add(lblCharName);
		btnHard = new JButton("Hard");
		btnHard.setBounds(362, 93, 96, 29);
		btnHard.setFont(new Font("Krungthep", Font.PLAIN, 13));
add(btnHard);
		btnBrutal = new JButton("Brutal");
		btnBrutal.setBounds(362, 123, 96, 29);
		btnBrutal.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(btnBrutal);
		final JLabel lblPlayerSkills = new JLabel("Player Skills:");
		lblPlayerSkills.setBounds(8, 208, 148, 21);
		lblPlayerSkills.setFont(new Font("Krungthep", Font.PLAIN, 15));
		lblPlayerSkills.setForeground(Color.ORANGE);
add(lblPlayerSkills);
		final JLabel lblPointsLeftTo = new JLabel("Points Left to Use:");
		lblPointsLeftTo.setBounds(222, 209, 132, 19);
		lblPointsLeftTo.setForeground(Color.ORANGE);
		lblPointsLeftTo.setFont(new Font("Krungthep", Font.PLAIN, 13));
add(lblPointsLeftTo);
		lblpointsLeft = new JLabel(pointsLeft.toString());
		lblpointsLeft.setBounds(362, 209, 96, 19);
		lblpointsLeft.setForeground(Color.ORANGE);
		lblpointsLeft.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblpointsLeft);
		final JLabel lblFighter = new JLabel("Fighter");
		lblFighter.setBounds(103, 277, 53, 38);
		lblFighter.setFont(new Font("Krungthep", Font.PLAIN, 13));
		lblFighter.setForeground(Color.ORANGE);
		add(lblFighter);
		final JLabel lblPilot = new JLabel("Pilot");
		lblPilot.setBounds(123, 234, 33, 38);
		lblPilot.setFont(new Font("Krungthep", Font.PLAIN, 13));
		lblPilot.setForeground(Color.ORANGE);
		add(lblPilot);
		final JLabel lblTrader = new JLabel("Trader");
		lblTrader.setBounds(108, 320, 48, 38);
		lblTrader.setForeground(Color.ORANGE);
		lblTrader.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblTrader);
		final JLabel lblEngineer = new JLabel("Engineer");
		lblEngineer.setBounds(92, 363, 64, 38);
		lblEngineer.setForeground(Color.ORANGE);
		lblEngineer.setFont(new Font("Krungthep", Font.PLAIN, 13));
	    add(lblEngineer);
		
//Char Name Field
		textField = new JTextField();
		textField.setBounds(8, 42, 148, 29);
		add(textField);
		textField.setColumns(10);
		
//Radio Buttons
		rdbtnMale = new JRadioButton("Male");
		rdbtnMale.setBounds(92, 83, 67, 24);
		rdbtnMale.setFont(new Font("Krungthep", Font.PLAIN, 13));
		rdbtnMale.setForeground(Color.ORANGE);
		add(rdbtnMale);
		rdbtnFemale = new JRadioButton("Female");
		rdbtnFemale.setBounds(8, 83, 85, 24);
		rdbtnFemale.setFont(new Font("Krungthep", Font.PLAIN, 13));
		rdbtnFemale.setForeground(Color.ORANGE);
		add(rdbtnFemale);
		final ButtonGroup group = new ButtonGroup();
	    group.add(rdbtnMale);
	    group.add(rdbtnFemale);

//Difficulty Buttons
		btnEasy = new JButton("Easy");
		btnEasy.setBounds(362, 29, 96, 29);
		btnEasy.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(btnEasy);
		btnNormal = new JButton("Normal");
		btnNormal.setBounds(362, 63, 96, 29);
		btnNormal.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(btnNormal);
			
//Pilot Points and Labels	
		pilotSlider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, 
				SLIDERSMIN, SLIDERSMAX, SLIDERSINIT);
		pilotSlider.setBounds(164, 234, 190, 38);
		pilotSlider.setForeground(Color.ORANGE);
		pilotSlider.setBackground(new Color(128, 0, 0));
		add(pilotSlider);
		//Turn on labels at major tick marks.
		pilotSlider.setMajorTickSpacing(MAINTICKSPACING);
		pilotSlider.setMinorTickSpacing(MINORTICKSPACING);
		pilotSlider.setSnapToTicks(true);
		pilotSlider.setPaintTicks(true);
		
		pilotPoints = pilotSlider.getValue();
		lblPilotPoints = new JLabel(pilotPoints.toString());
		lblPilotPoints.setBounds(362, 234, 96, 38);
		lblPilotPoints.setForeground(Color.ORANGE);
		lblPilotPoints.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblPilotPoints);
			
//Fighter Points and Labels	
		fighterSlider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, SLIDERSMIN, 
				SLIDERSMAX, SLIDERSINIT);
		fighterSlider.setBounds(164, 277, 190, 38);
		add(fighterSlider);
		//Turn on labels at major tick marks.
		fighterSlider.setMajorTickSpacing(MAINTICKSPACING);
		fighterSlider.setMinorTickSpacing(MINORTICKSPACING);
		fighterSlider.setSnapToTicks(true);
		fighterSlider.setPaintTicks(true);
		
		fighterPoints = fighterSlider.getValue();
		lblFighterPoints = new JLabel(fighterPoints.toString());
		lblFighterPoints.setBounds(362, 277, 96, 38);
		lblFighterPoints.setForeground(Color.ORANGE);
		lblFighterPoints.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblFighterPoints);
					
//Trader Points and Labels							
		traderSlider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, SLIDERSMIN,
				SLIDERSMAX, SLIDERSINIT);
		traderSlider.setBounds(164, 320, 190, 38);
		add(traderSlider);
		//Turn on labels at major tick marks.
		traderSlider.setMajorTickSpacing(MAINTICKSPACING);
		traderSlider.setMinorTickSpacing(MINORTICKSPACING);
		traderSlider.setSnapToTicks(true);
		traderSlider.setPaintTicks(true);
							
		traderPoints = traderSlider.getValue();
		lblTraderPoints = new JLabel(traderPoints.toString());
		lblTraderPoints.setBounds(362, 320, 96, 38);
		lblTraderPoints.setForeground(Color.ORANGE);
		lblTraderPoints.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblTraderPoints);
									
//Engineer Points and Labels									
	    engineerSlider= new JSlider(javax.swing.SwingConstants.HORIZONTAL, SLIDERSMIN,
	    		SLIDERSMAX, SLIDERSINIT);
	    engineerSlider.setBounds(164, 363, 190, 38);
	    add(engineerSlider);
		//Turn on labels at major tick marks.
	    engineerSlider.setMajorTickSpacing(MAINTICKSPACING);
	    engineerSlider.setMinorTickSpacing(MINORTICKSPACING);
	    engineerSlider.setSnapToTicks(true);
		engineerSlider.setPaintTicks(true);
		
		engineerPoints = engineerSlider.getValue();
		lblEngineerPoints = new JLabel(engineerPoints.toString());
		lblEngineerPoints.setBounds(362, 363, 96, 38);
		lblEngineerPoints.setForeground(Color.ORANGE);
		lblEngineerPoints.setFont(new Font("Krungthep", Font.PLAIN, 13));
		add(lblEngineerPoints);
		
// Complete Button
		btnCharacterComplete = new JButton("Character Complete");
		btnCharacterComplete.setBounds(164, 425, 190, 29);
		btnCharacterComplete.setFont(new Font("Krungthep", Font.PLAIN, 13));
add(btnCharacterComplete);

		lblCharimghere = new JLabel("", spaceMan, SwingConstants.CENTER);
		lblCharimghere.setBounds(186, 26, 150, 176);
		add(lblCharimghere);
		
//		JLabel backgroundPic = new JLabel("", background, SwingConstants.LEFT);
//		backgroundPic.setBounds(5, 5, 600, 600);
//		add(backgroundPic);
		
//Turns off Sliders until a difficulty is set
		setSliders(false);
	
//Listeners
		final ButtonListener buttonListen = new ButtonListener();
		btnEasy.addActionListener(buttonListen);
		btnNormal.addActionListener(buttonListen);
		btnHard.addActionListener(buttonListen);
		btnCharacterComplete.addActionListener(buttonListen);
		
		final SliderListener slideListen = new SliderListener();
		pilotSlider.addChangeListener(slideListen);
		traderSlider.addChangeListener(slideListen);
		fighterSlider.addChangeListener(slideListen);
		engineerSlider.addChangeListener(slideListen);
		
		final RadioListener radioListen = new RadioListener();
		rdbtnMale.addActionListener(radioListen);
		rdbtnFemale.addActionListener(radioListen);
	}
	
	/**
	 * ALL the button listeners for MakePlayerWindow
	 * @author Whitney
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{
		/**
		 * Checks the events that occur
		 * @param event The events that occur
		 */
		public void actionPerformed (ActionEvent event){
			boolean slidersOn = false;
			if(event.getSource().equals(btnEasy)){
				difficulty = 1;
				allocatedPoints = 30;
				pointsLeft = 30;
				reloadPointsLeft();
				sliderReset();
				lbldiffCheck.setText("");
				slidersOn = true;
			}
			else if(event.getSource().equals(btnNormal)){
				difficulty = 2;
				allocatedPoints = 20;
				pointsLeft = 20;
				reloadPointsLeft();
				sliderReset();
				lbldiffCheck.setText("");
				slidersOn = true;
			}
			else if(event.getSource().equals(btnHard)){
				difficulty = 3;
				allocatedPoints = 10;
				pointsLeft = 10;
				reloadPointsLeft();
				sliderReset();
				lbldiffCheck.setText("");
				slidersOn = true;
			}
			else if(event.getSource().equals(btnBrutal)){
				difficulty = 4;
				allocatedPoints = 1;
				pointsLeft = 1;
				reloadPointsLeft();
				sliderReset();
				lbldiffCheck.setText("Good Luck");
				slidersOn = true;
			}
			else if(event.getSource().equals(btnCharacterComplete)){
				boolean display = false;
				playerName = textField.getText();
				String errorString = "Please Complete the Following: \n";
				if(difficulty == -1){
					errorString = errorString + "   Select Difficulty \n";
					display = true;
				}
				if(!boolFemale && !boolMale){
					errorString = errorString + "   Select a Gender \n";
					display = true;
				}
				if(playerName.compareTo("") == 0){
					errorString = errorString + "   Enter your Name \n";
					display = true;
				}
				if(pointsLeft > 0){
					errorString = errorString + "   Spend All Character Points \n";
					display = true;
				}
				if(pointsLeft < 0){
					errorString = errorString + "   Remove Some Character Points \n";
					display = true;
				}
				if(display){
					JOptionPane.showMessageDialog(null, errorString);
				}
				else{
				//Set the skillsArray for the get Info
					skillsArray[0] = pilotSlider.getValue();
					skillsArray[1] = fighterSlider.getValue();
					skillsArray[2] = traderSlider.getValue();
					skillsArray[3] = engineerSlider.getValue();
				//Notifies that the character is indeed complete
					charComplete = 1;
				}
			}
			if(slidersOn){
				setSliders(true);
			}
		
		}
		
		/**
		 * The toString for the ButtonListener
		 * @return Returns a space because the button listener doesn't need a toString. */
		public String toString(){
			return "";
			
		}


	}

	/**
	 * Toggles between character's gender
	 * @author Whitney
	 * @version 1.0
	 */
	private class RadioListener implements ActionListener{
		/**
		 * Handels the Events that occur
		 * @param event The events that occur
		 */
		public void actionPerformed (ActionEvent event){
			if(event.getSource().equals(rdbtnFemale)){
				boolFemale = true;
				boolMale = false;
				lblCharimghere.setIcon(spaceGal);
			}
			else{
				boolFemale = false;
				boolMale = true;
				lblCharimghere.setIcon(spaceMan);
			}
		}

		/**
		 * The toString for the ButtonListener
		 * @return Returns a space because the button listener doesn't need a toString. */
		public String toString(){
			return "";
			
		}
	}

	/**
	 * Handles the sliders for skill selection
	 * @author Whitney
	 * @version 1.0
	 */
	private class SliderListener implements ChangeListener {
			/**
			 * Handles the changing state of all Sliders
			 * @param event The event that occurs when a slider changes
			 */
			 public void stateChanged(ChangeEvent event) {
				 pointsLeft = allocatedPoints - pilotPoints - fighterPoints - 
						 traderPoints - engineerPoints;
				 reloadPointsLeft();
				 if(difficulty == -1){
					 pilotSlider.setEnabled(false);
					 engineerSlider.setEnabled(false);
					 traderSlider.setEnabled(false);
					 fighterSlider.setEnabled(false);
					 JOptionPane.showMessageDialog(null, "Please Select A Difficulty");
				 }
				 if(pointsLeft == 0 && difficulty != -1){
					 pilotSlider.setExtent(20);
					 engineerSlider.setExtent(20);
					 traderSlider.setExtent(20);
					 fighterSlider.setExtent(20);
				 }
				 if(pointsLeft != 0 && difficulty != -1){
					 pilotSlider.setExtent(0);
					 engineerSlider.setExtent(0);
					 traderSlider.setExtent(0);
					 fighterSlider.setExtent(0);
				 }
				 if(event.getSource().equals(pilotSlider)){
					 pilotPoints = pilotSlider.getValue();
					 reloadPointsLeft();
					 lblPilotPoints.setText(pilotPoints.toString());
					 instantiateExtents();
					
				 }
				 else if(event.getSource().equals(traderSlider)){
					 traderPoints = traderSlider.getValue();
					 reloadPointsLeft();
					 lblTraderPoints.setText(traderPoints.toString());
				instantiateExtents();

				 }
				 else if(event.getSource().equals(fighterSlider)){
					 fighterPoints = fighterSlider.getValue();
					 reloadPointsLeft();
					 lblFighterPoints.setText(fighterPoints.toString());
					 instantiateExtents();
				 }
				 else if(event.getSource().equals(engineerSlider)){
					 engineerPoints = engineerSlider.getValue();
					 reloadPointsLeft();
					 lblEngineerPoints.setText(engineerPoints.toString());
					 instantiateExtents();
				 }
			 }

			 /**
				 * The toString for the ButtonListener
				 * @return Returns a space because the button 
				 * listener doesn't need a toString. 
				 * */
				public String toString(){
					return "";
					
				}
	}
	
	/**
	 * This method gives access to all the player's submitted character information.
	 * Array[0] is a string with playerName
	 * Array[1] is the boolean concerning the player's gender T/F for Female
	 * Array[2] an integer array of the skill distribution
	 * Array[3] an integer value of the difficulty selected
	 * 
	 * @return an object array of all the player's information
	 */
	public Object[] getInfo(){
		final Object[] infoArray= new Object[4];
		infoArray[0] = playerName;
		infoArray[1] = boolFemale;
		//Skills array is built after the character is complete
		//skillsArray 1: pilot 2: fighter 3: trader 4: engineer
		infoArray[2] = skillsArray;
		infoArray[3] = difficulty;
		
		return infoArray;
	}

	/**
	 * If the character is complete, this method will return 1
	 * @return an int either 0 or 1
	 */
	public int getCharComplete(){
		return charComplete;
	}
	
	/**
	 * Resets the sliders to zero
	 */
	private void sliderReset(){
		pilotSlider.setValue(SLIDERSINIT);
		fighterSlider.setValue(SLIDERSINIT);
		engineerSlider.setValue(SLIDERSINIT);
		traderSlider.setValue(SLIDERSINIT);
	}
	
	/**
	 * Sets the pointsLeft value
	 */
	private void reloadPointsLeft(){
		pointsLeft = allocatedPoints - pilotPoints - 
				fighterPoints - traderPoints - engineerPoints;
		lblpointsLeft.setText(pointsLeft.toString());
	}
	
	/**
	 * Sets the sliders to a certain extent if all points have been spent
	 */
	private void instantiateExtents(){
		if(pointsLeft == 0 && difficulty != -1){
			 pilotSlider.setExtent(20);
			 engineerSlider.setExtent(20);
			 traderSlider.setExtent(20);
			 fighterSlider.setExtent(20);
		 }
	}

	/**
	 * Toggles sliders on and off
	 * 
	 * @param enable boolean for setting slider on or off. False disables the sliders.
	 */
	private void setSliders(boolean enable){
		pilotSlider.setEnabled(enable);
		engineerSlider.setEnabled(enable);
		traderSlider.setEnabled(enable);
		fighterSlider.setEnabled(enable);
		
		pilotSlider.setVisible(enable);
		engineerSlider.setVisible(enable);
		traderSlider.setVisible(enable);
		fighterSlider.setVisible(enable);
		
	}
}